<?php
$pathFile = __FILE__;
$dir = dirname($pathFile);
$configFile = '../application/configs/application.ini';

if(!file_exists($dir .'/' . $configFile ))
{ 
    die("Doesn't existed config file.");
}

$config = parse_ini_file($dir .'/' . $configFile, true);

if(empty($config["database : production"]) || !is_array($config["database : production"]))
{
    die("Doesn't existed settings for database");
}

$host = $config["database : production"]["database.params.host"];
$user = $config["database : production"]["database.params.username"];
$password = $config["database : production"]["database.params.password"];
$db = $config["database : production"]["database.params.dbname"];


class mySQL {
    
    protected $host;
    protected $user;
    protected $password;
    protected $db;
    protected $link;
    
    public function setHost($host)
    {
        $this->host = trim($host);
    }
    
    public function getHost()
    {
        return $this->host;
    }
    
    public function setUser($user)
    {
        $this->user = trim($user);
    }
    
    public function getUser()
    {
        return $this->user;
    }
    
    public function setPassword($password)
    {
        $this->password = trim($password);
    }
    
    public function getPassword()
    {
        return $this->password;
    }

    public function setDatabase($db)
    {
        $this->db = trim($db);
    }

    public function getDatabase()
    {
        return $this->db;
    }

    public function query($query)
    {
        if($this->link)
        {
            @mysql_query($query);
        }
    }
    
    public function connect()
    {
        $this->link = @mysql_connect($this->host, $this->user, $this->password);
        
        if(!$this->link)
        {
            throw new Exception('Could not connect: ' . mysql_error());
        }

        if(!@mysql_select_db($this->db))
        {
            throw new Exception("Unable to select $this->db: " . mysql_error());
        }
    }
    
    public function disconnect()
    {
        if($this->link)
        {
           @mysql_close($this->link);
        }
    }
    
}
try {
    $mysql = new mySQL();
    $mysql->setHost($host);
    $mysql->setUser($user);
    $mysql->setPassword($password);
    $mysql->setDatabase($db);
    $mysql->connect();
	$mysql->query("INSERT INTO `config`(`name`, `value`) VALUES('page_teaser_size', '200');");
    $mysql->query("CREATE TABLE `shopping_config` (             
                   `name` varchar(100) NOT NULL,              
                   `value` varchar(700) NOT NULL default '',  
                   PRIMARY KEY  (`name`)                      
                 )");

    $mysql->query("CREATE TABLE `shopping_groups` (                        
                   `id` int(11) NOT NULL auto_increment,                 
                   `name` varchar(255) NOT NULL,                         
                   PRIMARY KEY  (`id`)                                   
                 )");

    $mysql->query("CREATE TABLE `shopping_products` (
                     `id` int(11) NOT NULL auto_increment,
                     `name` varchar(255) NOT NULL,
                     `item_id` varchar(25) NOT NULL,
                     `price` varchar(25) NOT NULL,
                     `photo` varchar(255) NOT NULL,
                     `brand` varchar(255) default '',
                     `desc` varchar(500) default '',
                     `options` varchar(400) default '',
                     `related` varchar(300) default '',
                     `page_id` int(11) default '0',
                     PRIMARY KEY  (`id`)
                   )");

    $mysql->query("CREATE TABLE `shopping_products_groups` (
                            `id_product` int(11) NOT NULL,
                            `id_group` int(11) NOT NULL,
                            UNIQUE KEY `product_group_unique` (`id_product`,`id_group`),
                            KEY `id_product` (`id_product`),
                            KEY `id_group` (`id_group`)
                          ) ");

	$mysql->query("CREATE TABLE `shopping_quote` (
                  `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                  `name` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
                  `content` TEXT COLLATE utf8_unicode_ci,
                  `status` ENUM('new','sent','sold','lost') COLLATE utf8_unicode_ci NOT NULL DEFAULT 'new',
                  `int_msg` TEXT COLLATE utf8_unicode_ci,
                  `sc_id` INT(10) UNSIGNED NOT NULL,
                  `date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
                  PRIMARY KEY (`id`)
                ) ENGINE=INNODB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ");

	$mysql->query("CREATE TABLE `shopping_usercart` (
                     `id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,
                     `name` VARCHAR(64) COLLATE utf8_unicode_ci NOT NULL,
                     `email` VARCHAR(255) COLLATE utf8_unicode_ci NOT NULL,
                     `address1` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `address2` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `country` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `state` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `city` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `zip` VARCHAR(25) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `phone` VARCHAR(255) COLLATE utf8_unicode_ci DEFAULT NULL,
                     `comments` TEXT COLLATE utf8_unicode_ci,
                     `data` TEXT COLLATE utf8_unicode_ci,
                     `date` TIMESTAMP NULL DEFAULT CURRENT_TIMESTAMP,
                     PRIMARY KEY (`id`)
                   ) ENGINE=INNODB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
    $mysql->query("CREATE TABLE `shopping_brands` (
                    `id` int(11) NOT NULL auto_increment,
                    `name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL DEFAULT '',
                    PRIMARY KEY (`id`))");
    $mysql->query("CREATE TABLE `shopping_paypal_payments` (
                            `id` int(11) NOT NULL auto_increment,
                            `transaction_id` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `amount` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `payer_id` varchar(100) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `description` varchar(400) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_name` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_email` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_address` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_address1` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_country` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_city` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_state` varchar(255) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `billing_zip` varchar(12) character set utf8 collate utf8_unicode_ci NOT NULL default '',
                            `date` datetime NOT NULL,
                            PRIMARY KEY  (`id`)
                          ) ENGINE=MyISAM DEFAULT CHARSET=utf8 ");
	$mysql->query("ALTER TABLE `page` ADD COLUMN `targeted_key_phrase` VARCHAR(255) CHARSET utf8 COLLATE utf8_unicode_ci NULL AFTER `silo_id`;");
	$mysql->query("ALTER TABLE `category` CHANGE `name` `name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_unicode_ci NULL ;");
	$mysql->query("ALTER TABLE `plugin` ADD COLUMN `cache` ENUM('0','1') DEFAULT '0';");
	$mysql->query("UPDATE `container` SET `container`.`container_type_id` = 2 WHERE `container`.`page_id` = 0 AND `container`.`container_type_id` = 1");
	$mysql->query("CREATE TABLE `password_recovering` (
          `link` varchar(100) character set utf8 NOT NULL default '',
          `time` varchar(255) character set utf8 NOT NULL
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;");
	$mysql->query("INSERT INTO `config` VALUES('page_teaser_size',200);");
	$mysql->query("ALTER TABLE `category` CHANGE `name` `name` VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL ;");
	$mysql->query("alter table `page_has_fa` add column `order` int (10)UNSIGNED  NOT NULL  after `fa_id`");
	$mysql->query("ALTER TABLE `shopping_quote` ADD COLUMN `disclaimer` TEXT CHARSET utf8 COLLATE utf8_unicode_ci NULL AFTER `status`;");
    $mysql->query("alter table `shopping_products` add column `tax` enum ('0','1') DEFAULT '0' NOT NULL  COLLATE utf8_unicode_ci  after `related`");
    $mysql->query("CREATE TABLE `se_position` (
        `page_url` TEXT NOT NULL,
	`key` TEXT NOT NULL,
	`google_pos` TINYINT(3) UNSIGNED NOT NULL,
	`yahoo_pos` TINYINT(3) UNSIGNED NOT NULL,
	`date` TIMESTAMP NULL DEFAULT NULL
        )
        ENGINE=MyISAM
        ROW_FORMAT=DEFAULT;");
        $mysql->query("alter table `shopping_usercart` add column `company` varchar (255) DEFAULT '' NULL  COLLATE utf8_unicode_ci  after `email`");
        $mysql->query("alter table `shopping_quote` add column `shipping` varchar (10) DEFAULT '' NULL  COLLATE utf8_general_ci  after `sc_id`");
        $mysql->query("CREATE TABLE `shopping_shipping` (
                `cart_id` INT(10) UNSIGNED NOT NULL,
                `shipping_cost` VARCHAR(10) NOT NULL COLLATE 'utf8_unicode_ci',
                `shipping_type` VARCHAR(64) NOT NULL COLLATE 'utf8_unicode_ci',
                `shipping_address` TEXT NOT NULL COLLATE 'utf8_unicode_ci'
            )
            COLLATE='utf8_unicode_ci'
            ENGINE=InnoDB
            ROW_FORMAT=DEFAULT");
        /*$mysql->query("DROP TABLE IF EXISTS `thesaurus_en`;");
   $mysql->query("DROP TABLE IF EXISTS `role`");
   $mysql->query("CREATE TABLE `role` (
        `id` int(10) unsigned NOT NULL auto_increment,
        `name` varchar(45) default NULL,
        PRIMARY KEY  (`id`)
        ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8");
    $mysql->query("insert  into `role`(`id`,`name`) values (1,'admin'),(2,'user'),(3,'superadmin')");
	$mysql->query("CREATE TABLE `seo_data`( `page_id` INT UNSIGNED , `seo_top` LONGTEXT CHARSET utf8 COLLATE utf8_general_ci , `seo_bottom` LONGTEXT CHARSET utf8 COLLATE utf8_general_ci );");
	$mysql->query("ALTER TABLE `seo_data` ADD UNIQUE `page_id` (`page_id`);");
	$mysql->query("
		CREATE TABLE `silo` (
			`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
			`name` varchar(65) DEFAULT NULL,
			PRIMARY KEY (`id`)
		) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8;
	");
	$mysql->query("ALTER TABLE `page` ADD COLUMN `silo_id` INT UNSIGNED NULL AFTER `seosamba_rule_id`;");
    $mysql->query("CREATE TABLE `forms` (
          `id` int(10) unsigned NOT NULL auto_increment,
          `name` varchar(64) NOT NULL,
          `code` text NOT NULL,
          `email` varchar(64) NOT NULL,
          `successMessage` text NOT NULL,
          `errorMessage` text NOT NULL,
          `emailFrom` varchar(64) NOT NULL,
          `emailMessage` text NOT NULL,
          `useSMTP` enum('0','1') NOT NULL default '0',
          PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;");
    $mysql->query("ALTER TABLE `deeplink` ADD COLUMN `ext_ws_id` INT UNSIGNED NULL AFTER `nofollow`;");
    $mysql->query("ALTER TABLE `silo` ADD COLUMN `as_cat` ENUM('0','1') CHARSET utf8 COLLATE utf8_general_ci DEFAULT '0' NULL AFTER `name`;");
	$mysql->query("CREATE TABLE `whitelabel_services`( `cpanel_logo` VARCHAR(255) CHARSET utf8 COLLATE utf8_general_ci , `services_content` TEXT CHARSET utf8 COLLATE utf8_general_ci );");
    $mysql->query("ALTER TABLE `comment` ADD COLUMN publish TINYINT(1) DEFAULT 1;");
	$mysql->query("alter table `comment` add column user_email varchar(60) default '';");
	$mysql->query("alter table `comment` add column abuse tinyint(1) default 0;");
	$mysql->query("alter table `comment` add column user_sign varchar(40) default '';");
	$mysql->query("alter table `comment` add column user_website varchar(40) default '';");
	$mysql->query("alter table `comment` add column rank tinyint(3) default 0;");
	$mysql->query("alter table `comment` add column topic varchar(60) default '';");
	$mysql->query("alter table `comment` add column keyword varchar(60) default '';");
	$mysql->query("alter table `comment` add column notify tinyint(1) default 0;");*/
	
	$mysql->query("ALTER TABLE `deeplink_content` ADD UNIQUE `deeplink_id` (`deeplink_id`, `content_id`);");

	//20.05.2010
	$mysql->query("ALTER TABLE `forms` ADD COLUMN `auto_reply_subj` VARCHAR(255) CHARSET utf8 COLLATE utf8_unicode_ci AFTER `emailFrom`;");
	$mysql->query("ALTER TABLE `page` ADD COLUMN `protected` ENUM('0','1') CHARSET utf8 COLLATE utf8_general_ci DEFAULT '0' AFTER `targeted_key_phrase`;");
	$mysql->query("INSERT INTO `role`(`id`, `name`) VALUES (4, 'member');");

    $mysql->query("ALTER TABLE `shopping_products`  ADD COLUMN `weight` VARCHAR(25) NULL AFTER `photo`;");
    $mysql->query("alter table `content` add column `blog` enum ('0','1') DEFAULT '0' NOT NULL  after `value`");
    $mysql->query("CREATE TABLE `ping_service` (
          `id` int(10) unsigned NOT NULL auto_increment,
          `url` varchar(255) collate utf8_unicode_ci default '',
          PRIMARY KEY  (`id`)
        ) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci");
    $mysql->query("insert into ping_service(`url`) values ('http://blogsearch.google.com/ping/RPC2', 'http://rpc.pingomatic.com/')");
    $mysql->query("insert  into `forms`(`id`,`name`,`code`,`email`,`successMessage`,`errorMessage`,`emailFrom`,`auto_reply_subj`,`emailMessage`,`useSMTP`) values (1,'contact','<p><label for=\"name\">Your Name: *</label><input name=\"name\" id=\"name\" type=\"text\" value=\"\" size=\"20\" maxlength=\"150\" /></p>\r\n<p><label for=\"email\">E-mail Address: *</label><input name=\"email\" type=\"text\" id=\"email\" size=\"20\" maxlength=\"100\" /></p>\r\n<p><label for=\"message\">Message: *</label><textarea name=\"message\" id=\"message\" cols=\"40\" rows=\"4\"></textarea></p>','tmp@seosamba.com','Success','Error','tmp@seosamba.com','','','1'),(2,'contact_detailed','<p align=\"left\"><label for=\"subject\">Subject: *</label><input name=\"subject\" type=\"text\" id=\"subject\" size=\"20\" maxlength=\"100\" /></p>\r\n<p align=\"left\"><label for=\"name\">Name: *</label><input name=\"name\" type=\"text\" id=\"name\" value=\"\" size=\"30\" maxlength=\"150\"></p>\r\n<p align=\"left\"><label for=\"email\">E-mail Address: *</label><input name=\"email\" type=\"text\" id=\"email\" size=\"20\" maxlength=\"100\" /></p>\r\n<p align=\"left\"><label for=\"address\">Mailing Address:</label><textarea name=\"address\" cols=\"30\" rows=\"3\"></textarea></p>\r\n<p align=\"left\"><label for=\"city\">City:</label><input name=\"city\" type=\"text\" id=\"city\" value=\"\" size=\"20\" maxlength=\"150\"></p>\r\n<p align=\"left\"><label for=\"state\">State:</label><select name=\"state\"> <option value=\"Alabama\">Alabama</option> <option value=\"Alaska\">Alaska</option> <option value=\"Arizona\">Arizona</option> <option value=\"Arkansas\">Arkansas</option> <option value=\"California\">California</option> <option value=\"Colorado\">Colorado</option> <option value=\"Connecticut\">Connecticut</option> <option value=\"Delaware\">Delaware</option> <option value=\"Florida\">Florida</option> <option value=\"Georgia\">Georgia</option> <option value=\"Hawaii\">Hawaii</option> <option value=\"Idaho\">Idaho</option> <option value=\"Illinois\">Illinois</option> <option value=\"Indiana\">Indiana</option> <option value=\"Iowa\">Iowa</option> <option value=\"Kansas\">Kansas</option> <option value=\"Kentucky\">Kentucky</option> <option value=\"Louisiana\">Louisiana</option> <option value=\"Maine\">Maine</option> <option value=\"Maryland\">Maryland</option> <option value=\"Massachusetts\">Massachusetts</option> <option value=\"Michigan\">Michigan</option> <option value=\"Minnesota\">Minnesota</option> <option value=\"Mississippi\">Mississippi</option> <option value=\"Missouri\">Missouri</option> <option value=\"Montana\">Montana</option> <option value=\"Nebraska\">Nebraska</option> <option value=\"Nevada\">Nevada</option> <option value=\"New Hampshire\">New Hampshire</option> <option value=\"New Jersey\">New Jersey</option> <option value=\"New Mexico\">New Mexico</option> <option value=\"New York\">New York</option> <option value=\"North Carolina\">North Carolina</option> <option value=\"North Dakota\">North Dakota</option> <option value=\"Ohio\">Ohio</option> <option value=\"Oklahoma\">Oklahoma</option> <option value=\"Oregon\">Oregon</option> <option value=\"Pennsylvania\">Pennsylvania</option> <option value=\"Rhode Island\">Rhode Island</option> <option value=\"South Carolina\">South Carolina</option> <option value=\"South Dakota\">South Dakota</option> <option value=\"Tennessee\">Tennessee</option> <option value=\"Texas\">Texas</option> <option value=\"Utah\">Utah</option> <option value=\"Vermont\">Vermont</option> <option value=\"Virginia\">Virginia</option> <option value=\"Washington\">Washington</option> <option value=\"Washington, DC\">Washington, DC</option> <option value=\"West Virginia\">West Virginia</option> <option value=\"Wisconsin\">Wisconsin</option> <option value=\"Wyoming\">Wyoming</option> <option value=\"\">--Territories--</option> <option value=\"American Samoa\">American Samoa</option> <option value=\"Federated States of Micronesia\">Federated States of Micronesia</option> <option value=\"Guam\">Guam</option> <option value=\"Midway Islands\">Midway Islands</option> <option value=\"Puerto Rico\">Puerto Rico</option> <option value=\"U.S. Virgin Islands\">U.S. Virgin Islands</option> </select></p>\r\n<p align=\"left\"><label for=\"zip\">Zip Code:</label><input name=\"zip\" type=\"text\" value=\"\" size=\"10\" maxlength=\"20\" /></p>\r\n<p align=\"left\"><label for=\"phone\">Phone:</label><input name=\"phone\" type=\"text\" id=\"phone\" value=\"(xxx) xxx - xxxx\" size=\"30\" maxlength=\"150\"></p>\r\n<p align=\"left\"><label for=\"message\">Message:</label><textarea name=\"message\" cols=\"45\" rows=\"6\"></textarea></p>','tmp@seosamba.com','Success','Error','tmp@seosamba.com','Subject','','1')");
	$mysql->query("ALTER TABLE `shopping_config`  CHANGE COLUMN `value` `value` TEXT NOT NULL DEFAULT '' AFTER `name`");
    $mysql->query("INSERT INTO `shopping_config` (name, value) VALUES ('tax-labels','a:4:{i:0;s:11:\"non taxable\";i:1;s:7:\"Default\";i:2;s:13:\"Alternative 1\";i:3;s:14:\"Alternative  2\";}') ON DUPLICATE KEY UPDATE name = 'tax-labels', value = 'a:4:{i:0;s:11:\"non taxable\";i:1;s:7:\"Default\";i:2;s:13:\"Alternative 1\";i:3;s:14:\"Alternative  2\";}';");
    $mysql->query("ALTER TABLE `shopping_products`  CHANGE COLUMN `tax` `tax` ENUM('0','1','2','3') NOT NULL DEFAULT '0' COLLATE 'utf8_unicode_ci' AFTER `related`");

    $mysql->query("ALTER TABLE `shopping_paypal_payments`  ADD COLUMN `sc_id` INT(10) UNSIGNED NOT NULL AFTER `date`;");
    $mysql->disconnect();
}
catch (Exception $e)
{
    echo $e->getMessage();
}


?>